package com.sghd.candy.module.element.service;

import com.sghd.candy.module.bag.manager.PlayerBag;
import com.sghd.candy.module.bag.manager.PlayerBagManager;
import com.sghd.candy.module.element.manager.*;
import com.sghd.candy.module.player.manager.Player;
import com.sghd.candy.module.player.manager.PlayerManager;
import com.sghd.candy.module.player.manager.PlayerOther;
import com.sghd.common.ramcache.aop.AutoLocked;
import com.sghd.common.ramcache.aop.IsLocked;
import com.sghd.pojo.StaticConfig;
import com.sghd.pojo.candy.bag.enums.PropType;
import com.sghd.pojo.fight.game.ChessType;
import com.sghd.pojo.fight.game.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import static com.sghd.pojo.StaticConfig.OTHER_DROP_MONEY;

/**
 * @author Allen
 * @date 2018/12/21
 */
@Service
public class PlayerElementService {
    private static final Logger logger = LoggerFactory.getLogger(PlayerElementService.class);
    @Autowired
    private PlayerElementManager playerElementManager;
    @Autowired
    private ElementConfigManager elementConfigManager;
    @Autowired
    private PlayerBagManager playerBagManager;
    @Autowired
    private PlayerManager playerManager;

    public PlayerElement getPlayerElement(long playerId) {
        return playerElementManager.getPlayerElement(playerId);
    }

    @AutoLocked
    public void change(@IsLocked Player player, int changeNum) {
        PlayerElement playerElement = playerElementManager.getPlayerElement(player.getPlayerId());
        playerElementManager.change(playerElement, changeNum);
    }

    @AutoLocked
    public Result<String> upgrade(@IsLocked Player player, ChessType chessType) {
        PlayerElement playerElement = playerElementManager.getPlayerElement(player.getPlayerId());
        int currentLevel = playerElement.getElements().getOrDefault(chessType,0);
        if (currentLevel == StaticConfig.ELEMENT_LEVEL_MAX) {
            if (logger.isDebugEnabled()) {
                logger.debug("玩家ID[{}]，升级五行失败，五行[{}]已最高级！");
            }
            return Result.valueOf(-1,"该五行已达到最高等级！");
        }

        ElementConfig elementConfig = elementConfigManager.getElementConfig(chessType);
        UpgradeEle upgradeEle = elementConfig.getUpgradeConfig().get(currentLevel + 1);

        int ownDianShu = playerElement.getDianShu();
        if (ownDianShu < upgradeEle.getDianshu()) {
            if (logger.isDebugEnabled()) {
                logger.debug("玩家ID[{}]，升级五行失败，五行[{}]点数不足！");
            }
            return Result.valueOf(-1,"该五行点数不足！");
        }

        PlayerBag playerBag = playerBagManager.getPlayerBag(player.getPlayerId());
        if (playerBag.getMoney() < upgradeEle.getMoney()) {
            if (logger.isDebugEnabled()) {
                logger.debug("玩家ID[{}]，升级五行失败，背包金币不足！");
            }
            return Result.valueOf(-1,"背包金币不足！");
        }

        playerElementManager.upgrade(playerElement, chessType);

        // 扣除
        playerElementManager.change(playerElement,-1 * upgradeEle.getDianshu());
        playerBagManager.change(playerBag, PropType.MONEY, -1 * upgradeEle.getMoney());

        currentLevel ++;
        PlayerOther other = playerManager.loadOther(player.getPlayerId());
        float dropMoneyRate = other.getContent(OTHER_DROP_MONEY,0F, Float.class);
        // 金币掉落额外加成
        // 火1林3山3，9风2雷3
        switch (chessType) {
            case CT_RED:
                if (currentLevel == 1) {
                    playerManager.update(other, OTHER_DROP_MONEY, dropMoneyRate + 0.02F);
                }
                break;
            case CT_BLUE:
                if (currentLevel == 2) {
                    playerManager.update(other, OTHER_DROP_MONEY, dropMoneyRate + 0.02F);
                }
                break;
            case CT_GREEN:
                if (currentLevel == 3) {
                    playerManager.update(other, OTHER_DROP_MONEY, dropMoneyRate + 0.02F);
                }
                break;
            case CT_PURPLE:
                if (currentLevel == 3) {
                    playerManager.update(other, OTHER_DROP_MONEY, dropMoneyRate + 0.02F);
                }
                break;
            case CT_YELLOW:
                if (currentLevel == 3 || currentLevel == 9) {
                    playerManager.update(other, OTHER_DROP_MONEY, dropMoneyRate + 0.02F);
                }
                break;
            default:
        }
        return Result.SUCCESS();
    }
}
